package com.hyzh.latte.sandtable.utils;

import com.hyzh.latte.sandtable.pojo.Graph.PointNode;
import com.hyzh.latte.sandtable.pojo.Vector_Line_2D;
import com.hyzh.latte.sandtable.pojo.Vector_Point_2D;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;


public class VectorUtils {
    public static Boolean isNodeExistNodeList(Vector_Point_2D node, ArrayList<Vector_Point_2D> nodeList){
        return nodeList.stream().anyMatch(element ->
                element.getX() == node.getX() && element.getY() == node.getY());
    }
    public static ArrayList<Vector_Point_2D> generateNodeFromLine(ArrayList<Vector_Line_2D> lines){
        ArrayList<Vector_Point_2D> resultNodes = new ArrayList<>();
        for (int i=0;i<lines.size();i++){
            Vector_Line_2D line = lines.get(i);
            Vector_Point_2D tempPoint0 = new Vector_Point_2D(line.getStartX(),line.getStartY());
            if (!isNodeExistNodeList(tempPoint0,resultNodes)){
                resultNodes.add(tempPoint0);
            }
            Vector_Point_2D tempPoint1 = new Vector_Point_2D(line.getEndX(),line.getEndY());
            if (!isNodeExistNodeList(tempPoint1,resultNodes)){
                resultNodes.add(tempPoint1);
            }
        }
        return resultNodes;
    }

    public static Integer findIndexFromNodeList(Vector_Point_2D target,ArrayList<Vector_Point_2D> list){
        for (int i = 0; i < list.size(); i++) {
            Vector_Point_2D point = list.get(i);
            if (point.getX() == target.getX() && point.getY() == target.getY()) {
                return ++i;
            }
        }
        return -1;
    }

    public static Vector_Point_2D stringToVectorPoint2D(String str) {
        str = str.replace("POINT(", "").replace(")", "");
        String[] parts = str.split(" ");
        if (parts.length != 2) {
            throw new IllegalArgumentException("Invalid input string. Must contain exactly one comma.");
        }
        double x = Double.parseDouble(parts[0]);
        double y = Double.parseDouble(parts[1]);

        Vector_Point_2D point = new Vector_Point_2D();
        point.setX(x);
        point.setY(y);
        return point;
    }

    /**
     * 将Collection<Map<String, Object>>集合转换为ArrayList<PointNode>集合
     * @param collection 需要转换的Collection<Map<String, Object>>集合
     * @return 转换后的ArrayList<PointNode>集合
     */
    public static ArrayList<PointNode> graphResultToPointNode(Collection<Map<String, Object>> collection){
        ArrayList<PointNode> result = new ArrayList<>();
        for (Map<String, Object> map : collection) {
            List<Long> indexes = (List<Long>) map.get("indexes");
            List<Double> xs = (List<Double>) map.get("xs");
            List<Double> ys = (List<Double>) map.get("ys");

            // 验证indexes、xs和ys的大小是否相同
            if (indexes.size() != xs.size() || indexes.size() != ys.size()) {
                throw new IllegalArgumentException("Indexes, xs and ys must have the same size");
            }

            for (int i = 0; i < indexes.size(); i++) {
                Long indexLong = indexes.get(i);
                Double x = xs.get(i);
                Double y = ys.get(i);

                Integer index = indexLong.intValue();

                Vector_Point_2D point = new Vector_Point_2D(x, y);
                PointNode pointNode = new PointNode(point, index);
                result.add(pointNode);
            }
        }
        return result;
    }
}
